/**
 * 
 */
package ec.app.deferEvalCoevolve1;

import ec.EvolutionState;
import ec.Evolve;
import ec.Individual;
import ec.util.Output;
import ec.util.ParameterDatabase;
import ec.vector.DoubleVectorIndividual;
import junit.framework.TestCase;

/**
 * @author RothmD01
 *
 */
public class DeferEval_TournamentSelection_simple_test extends TestCase {

	/**
	 * @param name
	 */
	public DeferEval_TournamentSelection_simple_test(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#setup(ec.EvolutionState, ec.util.Parameter)}.
	 */
	public void testSetup() {
		assertTrue("Not yet implemented", true); // TODO untested haven't tested all the setup yet... 
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#produce(int, int, int, int, ec.Individual[], ec.EvolutionState, int)}.
	 */
	public void testProduceIntIntIntIntIndividualArrayEvolutionStateInt() {
		ParameterDatabase pBase;
		String[] parms = new String[2];
		parms[0] = new String("-file");
		parms[1] = new String("src/ec/app/deferEvalCoevolve1/coevolve2Var2.params");
		EvolutionState state;
		pBase = Evolve.loadParameterDatabase(parms);
		state = Evolve.initialize(pBase, 1);
		state.setup(state, null );
		state.population = state.initializer.initialPopulation(state, 0); // unthreaded

		DeferEval_TournamentSelection_simple sel;
		sel = new DeferEval_TournamentSelection_simple();
		sel.size = 6;
		int retVal = -1;
		Individual retIndiv[];
		retIndiv = new Individual[8];
		//		produce(final int min, final int max, final int start, final int subpopulation, Individual[] inds, final EvolutionState state, final int thread) 
		retVal = sel.produce(1, 1, 0, 0, retIndiv, state, 0); 
		assertTrue("pop 0 produceN != 1", retVal == 1);

		retVal = sel.produce(1, 1, 0, 1, retIndiv, state, 0); 
		assertTrue("pop 1 produceN != 1", retVal == 1);	

		retVal = sel.produce(3, 6, 1, 1, retIndiv, state, 0); 
		assertTrue("pop 1 produceN 1-6 != 3", retVal == 3);		}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#produce(int, ec.EvolutionState, int)}.
	 */
	public void testProduceIntEvolutionStateInt() {
		ParameterDatabase pBase;
		String[] parms = new String[2];
		parms[0] = new String("-file");
		parms[1] = new String("src/ec/app/deferEvalCoevolve1/coevolve2Var2.params");
		EvolutionState state;
		pBase = Evolve.loadParameterDatabase(parms);
		state = Evolve.initialize(pBase, 1);
		state.setup(state, null );
        state.population = state.initializer.initialPopulation(state, 0); // unthreaded
		
		DeferEval_TournamentSelection_simple sel;
		sel = new DeferEval_TournamentSelection_simple();
		sel.size = 6;
		int retVal = -1;
		
		retVal = sel.produce( 0, state, 0);
		assertTrue("pop 0 produce != 5", retVal == 5);

		retVal = sel.produce( 1, state, 0);
		assertTrue("pop 1 produce != 12", retVal == 12);
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#defaultBase()}.
	 */
	public void testDefaultBase() {
		assertTrue("Not yet implemented", true); // TODO untested haven't tested all the default parameters yet... 
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#trial(ec.EvolutionState, ec.Subpopulation, ec.Individual[], ec.Individual[])}.
	 */
	public void testTrial() {

		ParameterDatabase pBase;
		String[] parms = new String[2];
		parms[0] = new String("-file");
		parms[1] = new String("src/ec/app/deferEvalCoevolve1/coevolve2Var2.params");
		EvolutionState state;
		pBase = Evolve.loadParameterDatabase(parms);
		state = Evolve.initialize(pBase, 1);
		state.setup(state, null );
        state.population = state.initializer.initialPopulation(state, 0); // unthreaded
        
		DoubleVectorIndividual tmA[], tmB[];
		tmA = new DoubleVectorIndividual[state.population.subpops.length];
		tmA[0] = (DoubleVectorIndividual) state.population.subpops[0].individuals[0];
		tmA[1] = (DoubleVectorIndividual) state.population.subpops[1].individuals[0];
		tmB = new DoubleVectorIndividual[state.population.subpops.length];
		tmB[0] = (DoubleVectorIndividual) state.population.subpops[0].individuals[1];
		tmB[1] = (DoubleVectorIndividual) state.population.subpops[1].individuals[1];
		
		DeferEval_TournamentSelection_simple sel;
		sel = new DeferEval_TournamentSelection_simple();
		double valA, valB;
		boolean retVal = false;
		CoevoRosenVar2 fxn;
    	fxn = (CoevoRosenVar2)(state.evaluator.p_problem);
    	valA = fxn.rosenbrock(tmA[0].genome[0], tmA[1].genome[0]);
    	valB = fxn.rosenbrock(tmB[0].genome[0], tmB[1].genome[0]);
    	retVal = sel.trial(state, state.population.subpops[0], tmA, tmB);
		Output outp = state.output;
		String outStr = retVal+"\ttmA: ("+valA+") " + tmA[0].genome[0] +" "+tmA[1].genome[0]+"\ttmB: ("+valB+") "+tmB[0].genome[0] +" "+tmB[1].genome[0];
		outp.message( outStr );
		
		assert retVal == (valA > valB);
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#evaluation(ec.EvolutionState, ec.Subpopulation, ec.Individual, ec.Individual)}.
	 */
	public void testEvaluation() {
		ParameterDatabase pBase;
		String[] parms = new String[2];
		parms[0] = new String("-file");
		parms[1] = new String("src/ec/app/deferEvalCoevolve1/coevolve2Var2.params");
		EvolutionState state;
		pBase = Evolve.loadParameterDatabase(parms);
		state = Evolve.initialize(pBase, 1);
		state.setup(state, null );
        state.population = state.initializer.initialPopulation(state, 0); // unthreaded
        
		DoubleVectorIndividual tmA[], tmB[];
		tmA = new DoubleVectorIndividual[state.population.subpops.length];
		tmA[0] = (DoubleVectorIndividual) state.population.subpops[0].individuals[0];
		tmA[1] = (DoubleVectorIndividual) state.population.subpops[1].individuals[0];
		tmB = new DoubleVectorIndividual[state.population.subpops.length];
		tmB[0] = (DoubleVectorIndividual) state.population.subpops[0].individuals[1];
		tmB[1] = (DoubleVectorIndividual) state.population.subpops[1].individuals[1];
		
		DeferEval_TournamentSelection_simple sel;
		sel = new DeferEval_TournamentSelection_simple();
		double valA, valB;
		boolean retVal = false;
		Individual indA1, indA2;
		indA1 = state.population.subpops[0].individuals[1];
		indA2 = state.population.subpops[0].individuals[2];
		
		retVal = sel.evaluation(state, state.population.subpops[0], indA1, indA2);
		assertTrue("indA1 !> indA2", retVal);

		indA1 = state.population.subpops[0].individuals[3];
		indA2 = state.population.subpops[0].individuals[4];
		
		retVal = sel.evaluation(state, state.population.subpops[0], indA1, indA2);
		assertTrue("indA3 !> indA4", retVal);

		//check if for the Bs...
		Individual indB1, indB2;
		indB1 = state.population.subpops[1].individuals[1];
		indB2 = state.population.subpops[1].individuals[2];
		
		retVal = sel.evaluation(state, state.population.subpops[1], indB1, indB2);
		assertTrue("indB1 !> indB2", retVal);

		indB1 = state.population.subpops[1].individuals[3];
		indB2 = state.population.subpops[1].individuals[4];
		
		retVal = sel.evaluation(state, state.population.subpops[1], indB1, indB2);
		assertTrue("indB3 !< indB4", !retVal);
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#teamSelect(ec.EvolutionState, ec.Subpopulation, ec.Individual)}.
	 */
	public void testTeamSelect() {
		ParameterDatabase pBase;
		String[] parms = new String[2];
		// NOTE: coevolve4Var2 is not a fully usable config file - it's used here to set up a population, but the evaluators
		//  are not configured generally enough to see 4 subpops
		parms[0] = new String("-file");
		parms[1] = new String("src/ec/app/deferEvalCoevolve1/coevolve4Var2.params");
		EvolutionState state;
		pBase = Evolve.loadParameterDatabase(parms);
		state = Evolve.initialize(pBase, 1);
		state.setup(state, null );
        state.population = state.initializer.initialPopulation(state, 0); // unthreaded
        
		DeferEval_TournamentSelection_simple sel;
		sel = new DeferEval_TournamentSelection_simple();
		
		Individual[] teamA, teamB;
		teamA = sel.teamSelect(state, state.population.subpops[0], state.population.subpops[0].individuals[0]);
		assertTrue( "TeamA simple select assert fails", teamA.length == state.population.subpops.length);
		teamB = sel.teamSelect(state, state.population.subpops[1], state.population.subpops[1].individuals[0]);
		assertTrue( "TeamB simple select assert fails", teamB.length == state.population.subpops.length);
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#seqTeamSelect(ec.EvolutionState, ec.Subpopulation, ec.Individual, int)}.
	 */
	public void testSeqTeamSelect() {
		ParameterDatabase pBase;
		String[] parms = new String[2];
		// NOTE: coevolve4Var2 is not a fully usable config file - it's used here to set up a population, but the evaluators
		//  are not configured generally enough to see 4 subpops
		parms[0] = new String("-file");
		parms[1] = new String("src/ec/app/deferEvalCoevolve1/coevolve4Var2.params");
		EvolutionState state;
		pBase = Evolve.loadParameterDatabase(parms);
		state = Evolve.initialize(pBase, 1);
		state.setup(state, null );
        state.population = state.initializer.initialPopulation(state, 0); // unthreaded
        
		DeferEval_TournamentSelection_simple sel;
		sel = new DeferEval_TournamentSelection_simple();
		
		Individual[][] teamA, teamB;
		teamA = sel.seqTeamSelect(state, state.population.subpops[0], state.population.subpops[0].individuals[0], 5);
		assertTrue( "TeamA simple select assert fails", teamA.length == 5);
		teamB = sel.seqTeamSelect(state, state.population.subpops[1], state.population.subpops[1].individuals[0], 5);
		assertTrue( "TeamB simple select assert fails", teamB.length == 5);

		boolean test;
		int test_len, team_len;
		teamA = sel.seqTeamSelect(state, state.population.subpops[0], state.population.subpops[0].individuals[0], 0);
		team_len = teamA.length;
		test_len = state.population.subpops[1].individuals.length;
		test = team_len == test_len;
		assertTrue( "TeamA convolution assert fails", teamA.length == 4096 );
		teamB = sel.seqTeamSelect(state, state.population.subpops[1], state.population.subpops[1].individuals[0], 255);
		team_len = teamB.length;
		test_len = state.population.subpops[0].individuals.length;
		test = team_len == test_len;
		assertTrue( "TeamB convolution assert fails", teamB.length == 255 );
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#individualReplaced(ec.steadystate.SteadyStateEvolutionState, int, int, int)}.
	 */
	public void testIndividualReplaced() {
		assertTrue("Not yet implemented", true); // TODO untested haven't tested this stub yet... 
	}

	/**
	 * Test method for {@link ec.app.deferEvalCoevolve1.DeferEval_TournamentSelection_simple#sourcesAreProperForm(ec.steadystate.SteadyStateEvolutionState)}.
	 */
	public void testSourcesAreProperForm() {
		assertTrue("Not yet implemented", true); // TODO untested haven't tested this stub method ... 
	}

}
